package org.wavetech.scrollingcascade;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.wavetech.adapter.FastAdapter;
import org.wavetech.adapter.ViewHolder;
import org.wavetech.scrollingcascade.layout.CascadeLayout;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    CascadeLayout cascadeLayout;
    RecyclerView recyclerView;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StatusBarHelper.setStatusBarStyle(this, Color.TRANSPARENT, true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerView);
        cascadeLayout = findViewById(R.id.title);
        textView = findViewById(R.id.text);
        textView.setOnClickListener(v -> {
            Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();
        });


        ArrayList<MenuItem> items = new ArrayList<>();
        Collections.addAll(items,
                new MenuItem("Look And Feel", "Skin, player interface, language, notifications"),
                new MenuItem("Audio", "Crossfade, replay gain, volume, output"),
                new MenuItem("Visualization", "Faded controls opacity, preset duration"),
                new MenuItem("Background", "Blur, details, intensity, saturation"),
                new MenuItem("Album Art", "Download, quality, cache cleanup"),
                new MenuItem("Library", "音乐库"),
                new MenuItem("Headset/Bluetooth", "耳机/蓝牙"),
                new MenuItem("Lock Screen", "锁屏"),
                new MenuItem("Misc", "其他"),
                new MenuItem("About", "关于我们"),
                new MenuItem("Get support", "获取支持"),
                new MenuItem("Export settings/data", "导出 设置/数据"),
                new MenuItem("Import settings/data", "导入 设置/数据"),
                new MenuItem("Send errors to developer", "发送错误给开发者")
        );
        FastAdapter<MenuItem> adapter = new FastAdapter<MenuItem>(this, R.layout.item_menu, items) {
            @Override
            public void bindData(ViewHolder holder, MenuItem menuItem) {
                holder.setText(R.id.title, menuItem.title);
                holder.setText(R.id.desc, menuItem.desc);
            }
        };
        View view = LayoutInflater.from(this).inflate(R.layout.item_desc, null);
        ((TextView) view.findViewById(R.id.desc)).setText("When ‘Sing: Come and Sing’ made an appearance in 2016, it immediately became a classic. It even managed to lift the statuette for Best Animated Film at the Golden Globe Awards in 2017.This fictional...");
        adapter.addFooterView(view);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        float density = getResources().getDisplayMetrics().density;
        float total = 120f * density;
        cascadeLayout.post(() -> {
//            recyclerView.setPadding(recyclerView.getPaddingLeft(), cascadeLayout.getHeight(), recyclerView.getPaddingRight(), recyclerView.getBottom());
            recyclerView.setClipToPadding(false);

            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {


                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }

                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int extent = recyclerView.computeVerticalScrollExtent();
                    int range = recyclerView.computeVerticalScrollRange();
                    int offset = recyclerView.computeVerticalScrollOffset();


                    float p = offset * 1f / total;
                    p = p > 1f ? 1 : p;

                    cascadeLayout.transition(p);

                    Log.e("------", String.format("extent:%s range:%s offset:%s", extent, range, offset));
                }
            });
            HeaderSnap.attach(recyclerView, (int) (density * 120), (int) (density * 20));
//            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//            if (layoutManager==null)return;
//            layoutManager.scrollToPosition(0);
        });
    }
}